package com.lsk.mytuan.entity;

import com.lsk.mytuan.util.Storageable;
import com.lsk.mytuan.util.Storager;

import static com.lsk.mytuan.util.FloatCalc.*;

public class User extends Storageable{
    private int id;
    private String username;
    private String password;
    private String role;
    private float wallet;
    private Storager orders;

    private static int idCounter = 0;
    private static Storager users = new Storager();

    public User (String username, String password, String role) {
        this.username = username;
        this.password = password;
        this.role = role;
        this.id = ++idCounter;
        this.wallet = 0F;
        this.orders = new Storager();
    }

    @Override
    public int getId() {
        return this.id;
    }

    public String getUsername () {
        return this.username;
    }

    public String getPassword () {
        return this.password;
    }

    public String getRole () {
        return this.role;
    }

    public Storager getOrders () {
        return this.orders;
    }

    public void setUsername (String username) {
        this.username = username;
    }

    public void setPassword (String password) {
        this.password = password;
    }

    public void addOrder (Order order) {
        this.orders.put(order);
    }

    public void addMoney (float money) {
        this.wallet = fadd(this.wallet, money);
    }

    public void showOrders (String state) {
        for (int i = 1; i <= this.orders.length(); i++) {
            Order order = (Order) this.orders.get(i);
            if (order != null && order.getState().equals(state)) {
                System.out.println(order);
            }
        }
    }

    public boolean canAfford (float money) {
        return this.wallet >= money;
    }

    public float pay (float money) {
        this.wallet = fdec(this.wallet, money);
        return this.wallet;
    }

    public static void register (String username, String password, String role) {
        if (! (role.equals("user") || role.equals("store"))) {
            System.out.println("角色不合法");
            return;
        }
        User user = new User(username, password, role);
        if (role.equals("store")) {
            Store store = new Store(user);
            store.add();
        }
        User.users.put(user);
    }

    public static User login (String username, String password) {
        for (int i = 1; i <= User.users.length(); i++) {
            User user = (User) User.users.get(i);
            if (user == null) {
                continue;
            }
            if (user.getUsername().equals(username)) {
                if (user.getPassword().equals(password)) {
                    return user;
                } else {
                    System.out.println("密码错误！");
                    return null;
                }
            }
        }
        System.out.println("用户不存在");
        return null;
    }

    @Override
    public String toString() {
        String result = "";
        result += "用户名：  " + this.username + "\r\n";
        result += "角色:     " + this.role + "\r\n";
        result += "钱包余额：" + this.wallet + "\r\n";
        return result;
    }
}
